package com.example.stock.ui.query;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;

import com.example.stock.R;
import com.example.stock.base.BaseActivity;
import com.example.stock.base.BaseViewModel;
import com.example.stock.databinding.ActivityQueryBinding;
import com.example.stock.model.GoodsOrderBean;
import com.google.android.material.tabs.TabLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

public class QueryActivity extends BaseActivity<ActivityQueryBinding, BaseViewModel> {

    private FragmentManager mFragmentManager;
    private List<Fragment> mFragments;

    private GoodsOrderBean orderBean;

    public static void start(Context context, GoodsOrderBean bean) {
        Intent intent = new Intent(context, QueryActivity.class);
        intent.putExtra("BEAN", bean);
        context.startActivity(intent);
    }

    @Override
    protected Class<BaseViewModel> initViewModel() {
        return BaseViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_query;
    }

    @Override
    protected void initView() {
        orderBean = (GoodsOrderBean) getIntent().getSerializableExtra("BEAN");
        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("已盘"));
        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("未盘"));
        mBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mBinding.vp.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mFragmentManager = getSupportFragmentManager();
        mFragments = new ArrayList<>();
        mFragments.add(FormFragment.getInstance(0));
        mFragments.add(FormFragment.getInstance(1));
        mBinding.vp.setAdapter(new FragmentPagerAdapter(mFragmentManager, 1) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        });
        mBinding.vp.setOffscreenPageLimit(mFragments.size());
    }

    public GoodsOrderBean getOrderBean() {
        return orderBean;
    }
}
